#include "kernel_operator.h"
using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2;    
template<typename TYPE_X, typename TYPE_Y> class Kernelxlogy {
    using T = TYPE_X;
public:
    __aicore__ inline Kernelxlogy() {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y,
                                uint32_t CoreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t TailDataNum) {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->coreDataNum = CoreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = TailDataNum;

        x1Gm.SetGlobalBuffer((__gm__ DTYPE_X1*)x1, this->coreDataNum);
        x2Gm.SetGlobalBuffer((__gm__ DTYPE_X2*)x2, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y*)y, this->coreDataNum);

        pipe.InitBuffer(inQueueX1, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_X1));
        pipe.InitBuffer(inQueueX2, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_X2));
        pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_Y));
    }
    __aicore__ inline void Process() {

        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        LocalTensor<DTYPE_X1> x1Local = inQueueX1.AllocTensor<DTYPE_X1>();
        LocalTensor<DTYPE_X2> x2Local = inQueueX2.AllocTensor<DTYPE_X2>();
        DataCopy(x1Local, x1Gm[progress * this->tileDataNum], this->processDataNum);
        DataCopy(x2Local, x2Gm[progress * this->tileDataNum], this->processDataNum);
        inQueueX1.EnQue(x1Local);
        inQueueX2.EnQue(x2Local);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        LocalTensor<DTYPE_X1> x1Local = inQueueX1.DeQue<DTYPE_X1>();
        LocalTensor<DTYPE_X2> x2Local = inQueueX2.DeQue<DTYPE_X2>();
        LocalTensor<DTYPE_Y> yLocal = outQueueY.AllocTensor<DTYPE_Y>();

        Ln(yLocal, x2Local, this->processDataNum);
        Mul(yLocal, yLocal, x1Local, this->processDataNum);
        outQueueY.EnQue<TYPE_Y>(yLocal);
        inQueueX1.FreeTensor(x1Local);
        inQueueX2.FreeTensor(x2Local);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        LocalTensor<DTYPE_Y> yLocal = outQueueY.DeQue<DTYPE_Y>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe pipe;
    TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX1, inQueueX2;
    TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    TBuf<QuePosition::VECCALC> QueueTmp1, QueueTmp2;

    GlobalTensor<DTYPE_X1> x1Gm;
    GlobalTensor<DTYPE_X2> x2Gm;
    GlobalTensor<DTYPE_Y> yGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

template<typename TYPE_X, typename TYPE_Y> class KernelxlogyBroadcast {
    using T = TYPE_X;
public:
    __aicore__ inline KernelxlogyBroadcast() {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y,
                                uint32_t CoreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t TailDataNum,
                                int32_t y_dimensional, 
                                int32_t *y_ndarray, int32_t *x1_ndarray, int32_t *x2_ndarray, 
                                int32_t *y_sumndarray, int32_t *x1_sumndarray, int32_t *x2_sumndarray ) {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->coreDataNum = CoreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = TailDataNum;

        this->y_dimensional = y_dimensional;

        for(int k=0; k<=y_dimensional; k++)
        {
            this->y_ndarray[k] = y_ndarray[k];
            this->x1_ndarray[k] = x1_ndarray[k];
            this->x2_ndarray[k] = x2_ndarray[k];
            this->y_sumndarray[k] = y_sumndarray[k];
            this->x1_sumndarray[k] = x1_sumndarray[k];
            this->x2_sumndarray[k] = x2_sumndarray[k];
        }

        x1Gm.SetGlobalBuffer((__gm__ DTYPE_X1*)x1, 1);
        x2Gm.SetGlobalBuffer((__gm__ DTYPE_X2*)x2, 1);
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y*)y, 1);

        pipe.InitBuffer(inQueueX1, BUFFER_NUM, this->y_sumndarray[this->y_dimensional] * sizeof(DTYPE_X1));
        pipe.InitBuffer(inQueueX2, BUFFER_NUM, this->y_sumndarray[this->y_dimensional] * sizeof(DTYPE_X2));
        pipe.InitBuffer(outQueueY, BUFFER_NUM, this->y_sumndarray[this->y_dimensional] * sizeof(DTYPE_Y));
    }
    __aicore__ inline void Process() {
        LocalTensor<DTYPE_X1> x1Local = inQueueX1.AllocTensor<DTYPE_X1>();
        LocalTensor<DTYPE_X2> x2Local = inQueueX2.AllocTensor<DTYPE_X2>();
        LocalTensor<DTYPE_Y> yLocal = outQueueY.AllocTensor<DTYPE_Y>();
        int dim = this->y_dimensional;
        
        for(int j=0; j<this->y_sumndarray[dim]; j++)
        {
            int x1_start = 0, x2_start = 0;
            for(int k=0; k<dim; k++)
            {
                if(this->x1_ndarray[k] != 1){
                    x1_start += this->x1_sumndarray[k] * (j / this->y_sumndarray[k] % this->y_ndarray[k]);
                }
                if(this->x2_ndarray[k] != 1){
                    x2_start += this->x2_sumndarray[k] * (j / this->y_sumndarray[k] % this->y_ndarray[k]);
                }

            }
            DTYPE_X1 x1 = x1Gm.GetValue(x1_start);
            DTYPE_X2 x2 = x2Gm.GetValue(x2_start);
            x1Local.SetValue(0, (DTYPE_Y)x1);
            x2Local.SetValue(0, (DTYPE_Y)x2);
            Ln(yLocal, x2Local, 1);
            Mul(yLocal, yLocal, x1Local, 1);
            yGm.SetValue(j, (DTYPE_Y)yLocal.GetValue(0));
            
        }
        inQueueX1.FreeTensor(x1Local);
        inQueueX2.FreeTensor(x2Local);
        outQueueY.FreeTensor(yLocal);
    }
    

private:
    TPipe pipe;
    TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX1, inQueueX2;
    TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    TBuf<QuePosition::VECCALC> QueueTmp1, QueueTmp2;

    GlobalTensor<DTYPE_X1> x1Gm;
    GlobalTensor<DTYPE_X2> x2Gm;
    GlobalTensor<DTYPE_Y> yGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
    
    int32_t y_dimensional;
    int32_t y_ndarray[20];
    int32_t x1_ndarray[20];
    int32_t x2_ndarray[20];

    int32_t y_sumndarray[20];
    int32_t x1_sumndarray[20];
    int32_t x2_sumndarray[20];
};
extern "C" __global__ __aicore__ void xlogy(GM_ADDR x1, GM_ADDR x2, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    // TODO: user kernel impl
    if(TILING_KEY_IS(1))
    {
        Kernelxlogy<DTYPE_X1, DTYPE_Y> op;
        op.Init(x1, x2, y, 
                tiling_data.CoreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.TailDataNum);  
        op.Process();
    }
    else if(TILING_KEY_IS(2))
    {
        KernelxlogyBroadcast<DTYPE_X1, DTYPE_Y> op;
        op.Init(x1, x2, y, 
                tiling_data.CoreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.TailDataNum,
                tiling_data.y_dimensional,
                tiling_data.y_ndarray, tiling_data.x1_ndarray, tiling_data.x2_ndarray,
                tiling_data.y_sumndarray, tiling_data.x1_sumndarray, tiling_data.x2_sumndarray);  
        op.Process();
    }
}